package net.lzzy.practices.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * @author 渊何
 */
public class FileUtils {
    public static String upload(MultipartFile file, String dir){
        try {
            String originName = file.getOriginalFilename();
            String suffix = Objects.requireNonNull(originName).substring(originName.lastIndexOf("."));
            String newName = UUID.randomUUID() + suffix;
            File f = new File(dir + newName);
            file.transferTo(f);
            return newName;
        }catch (IOException e){
            e.printStackTrace();
            return "";
        }
    }
}
